﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;
using FileSystem;
using System.Globalization;
using DataLoader;

namespace RedisDBBiz
{
    public class ReadRedisToCsv
    {
        public IRedisClient redisClient;
        public ReadRedisToCsv()
        {
            redisClient = CreateCacheClient();
        }

        public IRedisClient CreateCacheClient()
        {
            var client = new ServiceStack.Redis.BasicRedisClientManager("localhost").GetClient();
            return client;
        }

        public List<string> GetKeys()
        {
            List<string> keys = redisClient.SearchKeys(":*:P") ;
            return keys;
        }

        public void RedisToCsv(List<string> keys)
        {
            foreach (var key in keys)
            {
                List<string> priceList = redisClient.GetRangeFromList(key, 0, -1);
                int count = 0;
                int length = key.Length;
                int lastCom = key.LastIndexOf(':');
                string dateStr = key.Substring(lastCom - 8, 8);
                
                string format = "yyyyMMdd";
                CultureInfo provider = CultureInfo.InvariantCulture;
                DateTime dt = DateTime.ParseExact(dateStr, format, provider);

                string RoomStr = key.Substring(1);
                RoomStr = RoomStr.Substring(0, RoomStr.IndexOf(':'));
                
                foreach (var price in priceList)
                {
                    DateTime dtPrice = dt.AddDays(count);
                    string output = RoomStr + "," + dtPrice.ToString("yyyyMMdd") + "," + price;
                    NormalFileWriter.WriteTextToFile(output, "C:\\FileOutputSystem", "20140807Agoda.csv");
                    count++;
                }
            }
        }

        public void GetHotelCodeFile()
        {
            List<string> twodaysHotel = redisClient.GetRangeFromList("Min_LOS:2", 0, -1);
            List<string> threedaysHotel = redisClient.GetRangeFromList("Min_LOS:3", 0, -1);
            List<string> fourdaysHotel = redisClient.GetRangeFromList("Min_LOS:4", 0, -1);
            List<string> fivedaysHotel = redisClient.GetRangeFromList("Min_LOS:5", 0, -1);
            List<string> sixdaysHotel = redisClient.GetRangeFromList("Min_LOS:6", 0, -1);
            List<string> sevendaysHotel = redisClient.GetRangeFromList("Min_LOS:7", 0, -1);
            List<string> eightdaysHotel = redisClient.GetRangeFromList("Min_LOS:8", 0, -1);
            List<string> nightdaysHotel = redisClient.GetRangeFromList("Min_LOS:9", 0, -1);
            List<string> tendaysHotel = redisClient.GetRangeFromList("Min_LOS:10", 0, -1);
            List<string> ClosedHotel = redisClient.GetRangeFromList("Closed", 0, -1);

            OutPutFile(twodaysHotel, "Min_LOS_2_BookingHotelCode.txt");
            OutPutFile(threedaysHotel, "Min_LOS_3_BookingHotelCode.txt");
            OutPutFile(fourdaysHotel, "Min_LOS_4_BookingHotelCode.txt");
            OutPutFile(fivedaysHotel, "Min_LOS_5_BookingHotelCode.txt");
            OutPutFile(sixdaysHotel, "Min_LOS_6_BookingHotelCode.txt");
            OutPutFile(sevendaysHotel, "Min_LOS_7_BookingHotelCode.txt");
            OutPutFile(eightdaysHotel, "Min_LOS_8_BookingHotelCode.txt");
            OutPutFile(nightdaysHotel, "Min_LOS_9_BookingHotelCode.txt");
            OutPutFile(tendaysHotel, "Min_LOS_10_BookingHotelCode.txt");
            OutPutFile(ClosedHotel, "Closed_BookingHotelCode.txt");
        }
        private void OutPutFile(List<string> a, string fileName)
        {
            foreach (var line in a)
            {
                NormalFileWriter.WriteTextToFile(line, "C:\\FileOutputSystem", fileName);
            }
        }

        public void RedisToDB(List<string> keys)
        {
            foreach (var key in keys)
            {
                List<string> priceList = redisClient.GetRangeFromList(key, 0, -1);
                int count = 0;
                int length = key.Length;
                int lastCom = key.LastIndexOf(':');
                string dateStr = key.Substring(lastCom - 8, 8);

                string format = "yyyyMMdd";
                CultureInfo provider = CultureInfo.InvariantCulture;
                DateTime dt = DateTime.ParseExact(dateStr, format, provider);

                string RoomStr = key.Substring(1);
                RoomStr = RoomStr.Substring(0, RoomStr.IndexOf(':'));

                foreach (var price in priceList)
                {
                    DateTime dtPrice = dt.AddDays(count);
                    PriceLoader.InsertData(int.Parse(RoomStr), dtPrice, double.Parse(price));
                    //string output = RoomStr + "," + dtPrice.ToString("yyyyMMdd") + "," + price;
                    //NormalFileWriter.WriteTextToFile(output, "C:\\FileOutputSystem", "20140807Agoda.csv");
                    count++;
                }
            }
        }

    }
}
